package main

import (
	"BossSplider/models"
	"BossSplider/pkg/logging"
	"BossSplider/pkg/setting"
	"BossSplider/pkg/splider"
	"log"
	"time"

	"github.com/PuerkitoBio/goquery"
	"github.com/Unknwon/com"
)

var (
	baseUrl = map[string]string{
		"php": "https://www.zhipin.com/c101280100-p100103/?page=",
	}

	maxPage = 3000
)

//初始化统一控制方法
func serverInit() {
	setting.InitSetUp("conf/app.ini")
	logging.InitSetUp()
	models.InitSetUp()
}

//销毁统一控制
func serverDestory() {
	models.CloseDb()
}

func start(url string, page int, city string, types string) {
	body, response_status := splider.HttpGet(url+com.ToStr(page), "local")
	if response_status != 200 {
		log.Println("err", response_status)
	}

	doc, err := goquery.NewDocumentFromReader(body)
	if err != nil {
		log.Println("err", err)
	}

	dataList := splider.ParseList(doc)
	if len(dataList) > 0 {
		models.AddPosition(dataList, city, types)
		logging.Info("成功获取第" + com.ToStr(page) + "页数据")
	} else {
		log.Fatal("有验证码!")
	}

}

func main() {
	serverInit()
	defer serverDestory()

	url := baseUrl["php"]
	city := "广州"
	types := "php"
	for i := 1; i <= maxPage; i++ {
		go start(url, i, city, types)
		time.Sleep(5 * time.Second)
	}
}
